/*
 * Copyright (C) 2015 Miquel Sas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package traderlib.core.data.db.adapters;

import traderlib.core.data.db.Column;
import traderlib.core.data.db.DatabaseAdapter;
import traderlib.core.data.types.Types;

/**
 *
 * @author Miquel Sas
 */
public class OracleDatabaseAdapter extends DatabaseAdapter {

	/**
	 * Default constructor.
	 */
	public OracleDatabaseAdapter() {
		super();
		setDriverClassName("oracle.jdbc.driver.OracleDriver");
	}

	/**
	 * Returns the CURRENT DATE function as a string.
	 *
	 * @return The CURRENT DATE function as a string.
	 */
	@Override
	public String getCurrentDate() {
		return "SYSDATE";
	}

	/**
	 * Returns the CURRENT TIME function as a string.
	 *
	 * @return The CURRENT TIME function as a string.
	 */
	@Override
	public String getCurrentTime() {
		return "SYSDATE";
	}

	/**
	 * Returns the CURRENT TIMESTAMP function as a string.
	 *
	 * @return The CURRENT TIMESTAMP function as a string.
	 */
	@Override
	public String getCurrentTimestamp() {
		return "SYSDATE";
	}

	/**
	 * Gets the column definition to use in a <code>CREATE TABLE</code> statement, given a column.
	 *
	 * @return The column definition.
	 * @param column The column.
	 */
	@Override
	public String getColumnDefinition(Column column) {
		StringBuilder b = new StringBuilder();

		b.append(column.getNameCreate());
		b.append(" ");

		Types type = column.getType();
		if (type == Types.Boolean) {
			b.append("CHAR(1)");
		} else if (type == Types.ByteArray) {
			if (column.getLength() <= Types.fixedLength) {
				b.append("RAW");
				b.append("(");
				b.append(column.getLength());
				b.append(")");
			} else {
				b.append("LONG RAW");
			}
		} else if (type == Types.String) {
			if (column.getLength() <= Types.fixedLength) {
				b.append("VARCHAR2");
				b.append("(");
				b.append(Math.min(column.getLength(), Types.fixedLength));
				b.append(")");
			} else {
				b.append("LONG");
			}
		} else if (type == Types.Decimal) {
			b.append("NUMBER");
			b.append("(");
			b.append(column.getLength());
			b.append(",");
			b.append(column.getDecimals());
			b.append(")");
		} else if (type == Types.Double) {
			b.append("NUMBER");
		} else if (type == Types.Long) {
			b.append("NUMBER");
		} else if (type == Types.Integer) {
			b.append("NUMBER");
		} else if (type == Types.Date) {
			b.append("DATE");
		} else if (type == Types.Time) {
			b.append("DATE");
		} else if (type == Types.Timestamp) {
			b.append("DATE");
		} else {
			throw new IllegalArgumentException("Invalid column type to create the column");
		}

		return b.toString();
	}

	/**
	 * Check if the underlying database accepts explicit relations.
	 *
	 * @return A boolean.
	 */
	@Override
	public boolean isExplicitRelation() {
		return false;
	}

}
